﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;

namespace Page.Objects
{
    public class UserManagement : InternalPage
    {

        [FindsBy(How = How.Id, Using = "username")]
        public IWebElement UserNameField;

        [FindsBy(How = How.Name, Using = "email")] 
        public IWebElement EmailField;
 
        [FindsBy(How = How.Id, Using = "password")] 
        public IWebElement PassField;
            
        [FindsBy(How = How.Id, Using = "password2")] 
        public IWebElement Pass2Field;
         
        [FindsBy(How = How.Name, Using = "submit")] 
        public IWebElement SaveButton;

        [FindsBy(How = How.XPath, Using = "//div[h3='Users']//table//tbody//tr")] 
        public IList<IWebElement> UsersRows;

        public bool UserExists(string userName, string email)
        {
            foreach (IWebElement tr in UsersRows)
            {
                var tds = tr.FindElements(By.TagName("td"));
                if (tds[1].Text == userName && tds[2].Text == email)
                    return true;
            }

            return false;
        }

        public void DeleteUserIfExists(string userName)
        {
            foreach (var tr in UsersRows)
            {
                var tds = tr.FindElements(By.TagName("td"));
                if (tds[1].Text == userName)
                {
                    tds[5].Click();
                    break;
                }
            }
        }

    }
}
